<?php
/*
 * Created on Jan 16, 2013
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function frontend_channel_news_info(){
	return array(
        'news' => array(
            'title' => t('news'),
            'Description' => t('News channel description'),
            'access arguments' => array('access content'),
            'static' => 'yes',
            'menu' => 'news' // related menu
        ),
        
    );
}

function frontend_channel_news_settings(){
	
}
function frontend_channel_news(){
	$select = db_select('node', 'n')->extend('GloolsPagerDefault');
    $select->fields('n', array('nid', 'sticky', 'created'));
    $select->condition('n.type', 'article')
           ->condition('n.status', 1)
           ->condition('n.promote', 1)
           ->orderBy('n.sticky', 'DESC')
           ->orderBy('n.created', 'DESC')
           ->limit(variable_get('default_nodes_main', 10))
           ->addTag('node_access')
           ->addMetaData('page-owner', 'news');

  $nids = $select->execute()->fetchCol();

  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);
    $build = node_view_multiple($nodes, 'teaser');

    // 'rss.xml' is a path, not a file, registered in node_menu().
    drupal_add_feed('rss.xml', variable_get('site_name', 'Drupal') . ' ' . t('RSS'));
    $build['pager'] = array(
      '#theme'   => 'glools_pager',
      '#weight'  => 5,
      '#element' => 'news'
    );
    drupal_set_title('');
  }
  else {
    drupal_set_title(t('Welcome to @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), PASS_THROUGH);

    $default_message = '<p>' . t('No news has been created yet.') . '</p>';

    $default_links = array();
    if (_node_add_access()) {
      $default_links[] = l(t('Add new content'), 'node/add');
    }
    if (!empty($default_links)) {
      $default_message .= theme('item_list', array('items' => $default_links));
    }

    $build['default_message'] = array(
      '#markup' => $default_message,
      '#prefix' => '<div id="first-time">',
      '#suffix' => '</div>',
    );
  }
  return $build;
}

function frontend_channel_news_init(){
	return request_path();
}

/**
 * @return stdClass
 * - section: string
 * - data: array('options'=>array('global'=>$variables, '[private]'=>$variables))
 *         Private key values:
 *         $privateKey = array(
 *             $region.'.'.$info['view'].'.'.$info['private_key'],
 *             $region.'.'.$info['view'].'.'.$weight,
 *             $region.'.'.$info['view'],
 *             $info['view']
 *         );
 * */
function frontend_channel_news_viewPreset(&$vars, $path){
	
    $re = new stdClass();
    $re->section = 'news';
    $re->data = array();
    
    $re->data['options'] = array();
    
    return $re;
}

function frontend_channel_news_page_build(){
    //frontend_attach_clientLibForChannel('news');
    
    if (preg_match('/news\/[0-9]+/', request_path())) {
    	drupal_add_css(path_to_theme() . '/css/components/comment.com.css');
    }
}

/**
 * @Description:
 * The parameter [channel or channel/category] should be matched with request_path();
 * The path scan rule is the same with menu path.
 * e.g: "a/b/c" == request_path();
 *      Client library scan order: 
 *      1. "a/b/c"==[channel or channel/category]
 *      2. "a/b"==[channel or channel/category]
 *      3. "a"==[channel or channel/category]
 * The scan process would be break when any one of the client lib settings matched or nothing matched.
 * 
 * HOOK:
 * Be invoke by frontend_attach_clientLibForChannel();
 * */
function frontend_channel_news_clientLibSettings(){
	return array(
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        ),
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        )
    );
}